<!--

    Copyright (C) 2010-2012 the original author or authors.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Flyway Sample</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Flyway Sample">
    <meta name="author" content="The Flyway DEV Team">

    <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <style>
        h1 {
            padding-top: 10px;
        }

        p {
            padding-top: 10px;
            padding-bottom: 10px;
        }

        span {
            font-weight: bold;
        }
    </style>
</head>

<body>
<div class="container">

    <h1>Flyway Sample</h1>

    <br/>

    <p>
        AppServer: <span id="migration-appserver">UNKNOWN</span><br/>
        Database: <span id="migration-database">UNKNOWN</span>
    </p>

    <p>
        <button id="clean-button" class="btn btn-info btn-large">Clean</button>
        <button id="migrate-button" class="btn btn-info btn-large">Migrate</button>
    </p>

    <table class="table table-striped table-bordered">
        <thead>
        <tr>
            <th>Version</th>
            <th>Description</th>
            <th>Script</th>
            <th>Type</th>
            <th>Installed On</th>
            <th>Status</th>
        </tr>
        </thead>
        <tbody id="migration-table">
        <tr id="migration-none">
            <td colspan="6">No migrations found</td>
        </tr>

        </tbody>
    </table>

    <p>Version ${pom.version}</p>
</div>
<!-- /container -->

<script src="jquery/jquery-1.7.1.min.js"></script>
<script language="JavaScript">
    function loadEnvironmentInfo() {
        $.getJSON("env-info", function (data) {
            $("#migration-appserver").text(data.appserver);
            $("#migration-database").text(data.database);
        });
    }

    function refreshMigrationInfo() {
        $.get("info", function (data) {
            $("#migration-none").show();
            $(".migration-row").remove();

            $.each(data.rows, function (index, value) {
                $("#migration-none").hide();

                var tr = $("<tr class='migration-row'/>");
                tr.append("<td>" + value.version + "</td>");
                tr.append("<td>" + value.description + "</td>");
                tr.append("<td>" + value.script + "</td>");
                tr.append("<td>" + value.type + "</td>");
                tr.append("<td>" + value.installedOn + "</td>");
                tr.append("<td>" + value.state + "</td>");

                $("#migration-table").append(tr);
            });
        });
    }

    $(document).ready(function () {
        loadEnvironmentInfo();
        refreshMigrationInfo();

        $("#clean-button").click(function () {
            $.ajax({type:"POST", url:"clean", success:function (data) {
                refreshMigrationInfo();
            }});
        });

        $("#migrate-button").click(function () {
            $.ajax({type:"POST", url:"migrate", success:function (data) {
                refreshMigrationInfo();
            }});
        });
    });
</script>

</body>
</html>